﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Innova.Microfinanzas.BusinessEntities;
using MyCommerce.Presentation.Orders.Presenters;
using MyCommerce.Presentation.Orders.IViews;

namespace MyCommerce.Windows.Orders.Views
{
    public partial class NewProductDetail : Form, INewProductDetail
    {
        private NewProductDetailPresenter pPresenter;
        public NewProductDetail()
        {
            InitializeComponent();
            pPresenter = new NewProductDetailPresenter(this);
        }

        public static ProductNewOrderDetail ShowNewDetail(long idSupplier, long idMoney)
        {
            NewProductDetail objForm = new NewProductDetail();
            objForm.IdSupplierLoad = idSupplier;
            objForm.IdMoneyLoad = idMoney;
            objForm.ShowDialog();
            return objForm.ObjNewDetail;
        }

        public void DeployNewDetail(ProductPrice objPrice)
        {
            if (ObjProductSelected != null
                && Convert.ToInt32(fieldQuantity.Value) > 0)
            {
                ObjNewDetail = new ProductNewOrderDetail();
                ObjNewDetail.Quantity = Convert.ToInt32(fieldQuantity.Value);
                ObjNewDetail.ObjProduct = ObjProductSelected;
                ObjNewDetail.ObjPrice = objPrice;
                ObjNewDetail.TotalDetail = objPrice.PurchasePrice * Convert.ToDecimal(ObjNewDetail.Quantity);
                Close();
            }
        }

        public List<Product> ColProductsLoad 
        {
            set
            {
                listProducts.DataSource = value;
                listProducts.DataBind();
            }
        }

        public Product ObjProductSelected 
        {
            get
            {
                if (listProducts.ActiveRow != null
                              && listProducts.ActiveRow.Selected)
                    return (Product)listProducts.SelectedRow.ListObject;
                return null;
            }
        }

        public long IdSupplierLoad
        { get; set; }

        public long IdMoneyLoad
        { get; set; }

        public ProductNewOrderDetail ObjNewDetail
        {
            get;
            set;
        }

        public event EventHandler OnLoadView;
        public event EventHandler OnDeployDetail;

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (ObjProductSelected != null)
            {
                if (OnDeployDetail != null)
                    OnDeployDetail(this, new EventArgs());
            }
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            ObjNewDetail = null;
            Close();
        }

        private void NewProductDetail_Load(object sender, EventArgs e)
        {
            if (OnLoadView != null)
                OnLoadView(this, new EventArgs());
        }
    }
}
